﻿using System.Collections.Generic;

namespace Safebrowsing
{
    /// <summary>
    /// Represents chunk data.
    /// Read here for details: http://code.google.com/apis/safebrowsing/developers_guide_v2.html#ListContents
    /// </summary>
    public class ChunkData
    {
        public ChunkData(int hostKey)
        {
            HostKey = hostKey;
            Prefixes = new List<string>();
            SubDataPrefixes = new Dictionary<int, List<string>>();
        }

        /// <summary>
        /// 32-bit hash prefix for the host name
        /// </summary>
        public int HostKey { get; private set; }

        /// <summary>
        /// List of prefixes for this host key
        /// </summary>
        public List<string> Prefixes { get; private set; }

        /// <summary>
        /// List of sub-data prefixes.
        /// Key is add-chunk number, that contained this host key.
        /// Value is list of prefixes to remove from that chunk.
        /// </summary>
        public Dictionary<int, List<string>> SubDataPrefixes { get; private set; }

        /// <summary>
        /// Adds prefix to the host data
        /// </summary>
        /// <param name="prefix"></param>
        public void AddPrefix(byte[] prefix)
        {
            Prefixes.Add(Utils.ByteToHex(prefix));
        }

        /// <summary>
        /// Adds sub-chunk data.
        /// </summary>
        /// <param name="addChunkNumber"></param>
        /// <param name="prefix"></param>
        public void AddSubData(int addChunkNumber, byte[] prefix)
        {
            if (!SubDataPrefixes.ContainsKey(addChunkNumber))
            {
                SubDataPrefixes[addChunkNumber] = new List<string>();
            }

            if (prefix != null)
            {
                SubDataPrefixes[addChunkNumber].Add(Utils.ByteToHex(prefix));
            }
        }
    }
}
